<?php  

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class bank extends CI_Controller {
	function __construct()
	{
		parent::__construct();
	
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}		
		$this->load->library('form_validation');
		$this->load->model('accounts/bank_model');	
		$this->load->library('datatables');			
	}
		
		
	function get(){
		echo $this->accounts_model->get();
	}	
	
	function add_transaction()
	{
		$this->data['page_title'] = "Bank Transaction";
		$meta['page_title'] = "Bank Transaction";	
		$this->data['customers']=$this->bank_model->get_customers();
		$this->data['banks']=$this->bank_model->get_banks();
		$this->form_validation->set_rules('id_bank', 'Bank Name', 'required|is_natural_no_zero|xss_clean');
		$this->form_validation->set_rules('date', 'Date', 'required|xss_clean');
		$this->form_validation->set_rules('credit_amount', 'Credit Amount', 'required|xss_clean');
		if ($this->form_validation->run() == true) 
		{
			$id_account = $this->input->post('voucher_no');
			$data = array(
				'id_bank' => $this->input->post('id_bank'),
				'date' => $this->input->post('date'),
				'transaction_type' => $this->input->post('id_bank'),
				'voucher_no' => $this->accounts_model->get_voucherno($this->input->post('voucher_no')),
				'payment_type' => $this->input->post('payment_type'),
				'cheque_no' => $this->input->post('cheque_no'),
				'bank_name' => $this->input->post('bank_name'),
				'date' => $this->input->post('date'),
				'credit_amount' => $this->input->post('credit_amount'),
				'ip' => $_SERVER['REMOTE_ADDR'],
			);	
			if($id_account==''){
					$this->accounts_model->insert($data, $id_account);
				
			}else{
				$this->accounts_model->update_mr($data, $id_account);
			}
			redirect("accounts/money_receipt/view_mr");
		
		}
		else
		{
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['id_user'] = array(
				'name'  => 'id_user',
				'id'    => 'id_user',
				'type'  => 'select',
				'value' => $this->form_validation->set_value('id_user'),
				
			);

			$this->data['voucher_no'] = array(
				'name'  => 'voucher_no',
				'id'    => 'voucher_no',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('voucher_no'),
			);
	
			$this->data['date'] = array(
			'name'  => 'date',
			'id'    => 'date',
			'type'  => 'text',
			'class' => 'datepicker',
			'value' => $this->form_validation->set_value('date'),
			);
	
			$this->data['credit_amount'] = array(
			'name'  => 'credit_amount',
			'id'    => 'credit_amount',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('credit_amount'),
			);
		    $this->load->view('common/header',$meta);
			$this->load->view('accounts/money_receipt/create_mr', $this->data);
			$this->load->view('common/footer');
		}
	}
	
	function view_mr(){
		$this->data['payments'] = $this->accounts_model->get_payments()->result();
	   $meta['page_title'] = "Payment Lists";
	   $this->load->view('common/header', $meta);
	   $this->load->view('accounts/money_receipt/view_mr1');
       $this->load->view('common/footer');
	}
	

	function edit_mr($id)
	{
		
		$this->data['page_title'] = "Edit Money Receipt";
		if (!$this->ion_auth->logged_in())
		{
			redirect('auth', 'refresh');
		}

		$user = $this->accounts_model->edit_mr($id);
		$user['id_sale'] = $this->accounts_model->get_id_sale($user['voucher_no']);
		$this->data['customers']=$this->accounts_model->get_customers();
		$this->data['banks']=$this->payment_model->get_banks();
		$this->form_validation->set_rules('id_user', 'Customer', 'required|is_natural_no_zero|xss_clean');
		$this->form_validation->set_rules('date', 'Date', 'required|xss_clean');
		$this->form_validation->set_rules('credit_amount', 'Credit Amount', 'required|xss_clean');
		if ($this->form_validation->run() == true) 
		{
			
			$data = array(
				'id_user' => $this->input->post('id_user'),
				'user_name' => $this->accounts_model->get_partyname($this->input->post('id_user')),
				'voucher_type' => 'receipt',
				'voucher_no' => $this->accounts_model->get_voucherno($this->input->post('voucher_no')),
				'payment_type' => $this->input->post('payment_type'),
				'cheque_no' => $this->input->post('cheque_no'),
				'bank_name' => $this->input->post('bank_name'),
				'date' => $this->input->post('date'),
				'credit_amount' => $this->input->post('credit_amount'),
				'ip' => $_SERVER['REMOTE_ADDR'],
			);	
			if($data['payment_type']==0){
				$data['cheque_no']='';
				$data['bank_name'] = '';
			}
			$this->accounts_model->update($data, $id);
			redirect("accounts/money_receipt/view_mr");
		}
		else
		{
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['id_user'] = array(
				'name'  => 'id_user',
				'id'    => 'id_user',
				'type'  => 'select',
				'value' => $this->form_validation->set_value('id_user'),
				
			);

			$this->data['voucher_no'] = array(
				'name'  => 'voucher_no',
				'id'    => 'voucher_no',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('voucher_no'),
			);
	
			$this->data['date'] = array(
			'name'  => 'date',
			'id'    => 'date',
			'type'  => 'text',
			'class' => 'datepicker',
			'value' => $this->form_validation->set_value('date'),
			);
	
			$this->data['credit_amount'] = array(
			'name'  => 'credit_amount',
			'id'    => 'credit_amount',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('credit_amount'),
			);
		
			$this->data['user']=$user;
			$this->load->view('common/header');
			$this->load->view('accounts/money_receipt/edit_mr', $this->data);
			$this->load->view('common/footer');
		}
	}

	
	function get_invoiceno(){
	$invoices=$this->accounts_model->get_invoiceno($_REQUEST['id_user']);
	echo  $invoices;exit;	
	
	}



	function delete_mr($id){
		$this->accounts_model->delete_mr($id);
		redirect("accounts//money_receipt/view_mr");		
	}

function get_amount(){
	$credit_amount = $this->accounts_model->get_credit_amount($_REQUEST['id_acc']);
	echo $credit_amount;exit; 
}


}
?>